home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7639 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.9 KB

  1. Path: news.halcyon.com!usenet
  2. From: normanb@halcyon.com (Norm Bryar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: C calling C++ function !!!
  5. Date: Sat, 24 Feb 1996 19:14:55 GMT
  6. Organization: Northwest Nexus Inc.
  7. Message-ID: <4gno2b$t1b@news.halcyon.com>
  8. References: <4ghldd$rt@news1.usa.pipeline.com>
  9. NNTP-Posting-Host: blv-pm11-ip3.halcyon.com
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. grantp@usa.pipeline.com(Pete) wrote:
  13.  
  14. >On Feb 21, 1996 12:24:45 in article <Re: C calling C++ function !!!>,
  15. >'a-kovalenko@star.nmb.ru (Alexey Kovalenko)' wrote: 
  16. >>Hello, all. 
  17. >> 
  18. >>Few days ago Norm Bryar <normanb@halcyon.com> in his answer has written: 
  19. >> 
  20. >>>Something tells me it's not that simple.   
  21. >>> 
  22. >>[Skipped] 
  23. >>>I believe the accepted thing to do is define a set of 'C' wrapper APIs 
  24. >>>that you can export to your 'C' modules.  Internally, your wrappers do 
  25. >>>class method calls.   
  26. >> 
  27. >>It's wrong. Function in C++, defined as "C", just uses predefined C
  28. >calling  
  29. >>convention instead compiler's preferred. 
  30.  
  31. First "It's wrong..." is misleading, since someone elided the bulk of
  32. my message.
  33. You sidestep name mangling with extern "C", clearly, though I don't
  34. know the effects on overloaded functions.    But my mail also
  35. critiques someone's assertion that you could use ->vtabl or some other
  36. magic word to access the virtual function table inside a 'C' compiled
  37. module.  
  38.  
  39. >What?  Come again, please. 
  40. >> 
  41. >>Real solution here (don't say "It will not work" - I'm using this ): 
  42. >> 
  43. >Well, then you haven't posted all of the relevant code.  Even 
  44. >"filling the obvious blanks" will not produce workable code without 
  45. >and extern "C" somewhere.  In particular, the symbol Msg is defined 
  46. >in the c++ module, which is referenced in the c code.  C++ produces 
  47. >a mangled version while the c code looks for _Msg.  It won't link. 
  48. >>Situation : I'm working on project that contain both C and C++ sources and
  49. >I  
  50. >>don't want to make C sources C++. I'm needing to call function-classmember
  51. >>from C sources . Function must return char* by index(access to indexed
  52. >strings  
  53. >> 
  54. >>array, fuction in C used instead operator[] in C++).  
  55. >> 
  56. >>header contains: 
  57. >>typedef char * (*MsgFunc)(int iNo ); 
  58. >> 
  59. >>C++ file contains: 
  60. >> 
  61. >>#include "header.h" 
  62. >>#include "cassdef.hpp" 
  63. >> 
  64. >>static Message * _Msg = new Message("messages.msg"); 
  65. >>static Message & Msg1 = *_Msg; 
  66. >>// Two variables to avoid compiler warning about 
  67. >> 
  68. >>char * Msg( int iNo ) 
  69. >>{ 
  70. >>return Msg1[iNo];        // operator [] 
  71. >>} 
  72.  
  73. But for the missing extern "C" and missing instance parameter, this is
  74. what I meant: Define 'C' wrappers for the class method calls.
  75.  
  76.  
  77. >...
  78.  
  79. We've also failed to mention the COM model used by Microsoft for OLE,
  80. the Win95 shell, and practically every other toolkit they're coming
  81. out with recently (Quartz, Audioman, etc.)?  Look in objbase.h and
  82. you'll see an example of quite portable interface structure where no
  83. one cares what language either client or server was implemented in.  
  84.  
  85.                         --Norm 
  86.  
  87.